package com.techysky.util;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;

public class MD5Test {
	/**
	 * MD5方法
	 * 
	 * @param text明文
	 * @return 密文
	 * @throws Exception
	 */
	public static String md5(String text) throws Exception {
		// 加密后的字符串
		String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		String plaintext = text + time;
		String encodeStr = DigestUtils.md5Hex(plaintext + "ABCDFGH");
		String encodeStrUpper = encodeStr.toUpperCase();
		String str = encodeStrUpper.substring(encodeStrUpper.length() - 8, encodeStrUpper.length());
		return str;
	}
	
	static Random r = new Random();
	public static String randomEncode(String mw){
		if(mw.length()>8){
			mw = mw.substring(0, 8);
		}
		String temp = mw+r.nextInt(10000);
		return new String(Base64.getEncoder().encode(temp.getBytes()));
	}

	public static String randomDecoder(String decStr){
		String str = new String(Base64.getDecoder().decode(decStr.getBytes()));
		return str.substring(0, 8);
	}

	public static void main(String[] args) throws Exception {
		String mm = md5("FA-16-54-C9-AE-9788");//FA-16-54-C9-AE-97  88-88-88-88-88-06
		
		
		System.out.println("md5:"+mm);// abc
		String ss = randomEncode(mm);
		System.out.println(ss);
		System.out.println(randomDecoder(ss));
	
		
	}
}